package logic

import (
	"context"
	"fresh-box/models/mysql"

	"fresh-box/freshrpc/freshrpc"
	"fresh-box/freshrpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CategoryCreateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCategoryCreateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CategoryCreateLogic {
	return &CategoryCreateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 添加
func (l *CategoryCreateLogic) CategoryCreate(in *freshrpc.CategoryCreateRequest) (*freshrpc.CategoryCreateResponse, error) {
	// todo: add your logic here and delete this line
	var category mysql.Category
	if err := mysql.DB.Debug().Where("category=?", category).Error; err != nil {
		category = mysql.Category{
			Name: in.Name,
		}
	}
	if err := category.CategoryCreate(mysql.DB); err != nil {
		return nil, err
	}
	return &freshrpc.CategoryCreateResponse{
		CategoryId: int64(category.ID),
	}, nil
}
